//
//  LeftViewController.m
//  Everest
//
//  Created by 李其昌 on 14/12/18.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "LeftViewController.h"
#import "UIImageView+WebCache.h"
#import "Classify.h"
#import "ButtonCell.h"

@interface LeftViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *buttonArr;
@end

@implementation LeftViewController

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"findData"];
    [_tableView release];
    [_buttonArr release];
    [super dealloc];
}

- (id)init
{
    self = [super init];
    if (self) {
        [self setRestorationIdentifier:@"MMExampleLeftSideDrawerController"];
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(receiveNotification:) name:@"findData" object:nil];
    }
    return self;
}

- (void)receiveNotification:(NSNotification *)noti
{
    self.buttonArr = [noti.userInfo objectForKey:@"array"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 80, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 75, 0);
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[ButtonCell class] forCellReuseIdentifier:@"resues"];
    [_tableView release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.buttonArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"resues"];
    Classify *classify = [self.buttonArr objectAtIndex:indexPath.row];
    [cell.myImageView setImageWithURL:[NSURL URLWithString:classify.coverPath]];
    return cell;
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Classify *classify = [self.buttonArr objectAtIndex:indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"push" object:classify userInfo:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
